<template>
  <div>
    <van-pull-refresh v-model="isLoading" @refresh="onRefresh">
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
        :immediate-check="false"
      >
        <ArtItem
          v-for="item in artList"
          :key="item.art_id"
          :article="item"
          @remove-article="removeArticle"
        ></ArtItem>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
import { getArtListAPI } from '../../api/homeApi'
import ArtItem from '../ArtItem/ArtItem.vue'
export default {
  name: 'ArtList',
  props: {
    channelId: {
      type: Number,
      required: true
    }
  },
  components: {
    ArtItem
  },
  data () {
    return {
      artList: [],
      // 调用api要传递
      timestamp: Date.now(),
      loading: false,
      finished: false,
      // 是否在下拉刷新
      isLoading: false
    }
  },
  created () {
    this.initArtList()
  },
  methods: {
    async initArtList (isPullDown) {
      const { data: res } = await getArtListAPI(this.channelId, this.timestamp)
      console.log(res)
      if (res.message === 'OK') {
        // 为时间戳重新赋值
        this.timestamp = res.data.pre_timestamp
        // 为 artlist 赋值
        if (isPullDown) {
          this.artList = [...res.data.results, ...this.artList]
          this.isLoading = false
        } else {
          this.artList = [...this.artList, ...res.data.results]
          this.loading = false
        }

        if (res.data.pre_timestamp === null) {
          this.finished = true
          this.disabled = true
        }
      }
    },
    removeArticle (id) {
      this.artList = this.artList.filter(item => item.art_id !== id)
      if (this.artList.length < 10) {
        this.initArtList()
      }
    },
    onLoad () {
      this.initArtList()
    },
    onRefresh () {
      this.initArtList(true)
    }
  }

}
</script>

<style lang="scss" scoped>
</style>
